#include <semaphore.h>
#include <yatak/yatak.h>

int semaphore_init (sem_t * sem, uint32 max)
{
	if (sem == NULL)
		return -1;
	if (max <= 0)
		return -2;
	//if (max > MAX_SEMAPHORE)
	//	max = MAX_SEMAPHORE;
	sem->max = max;
	sem->cur = max;
	return 0;
}
int semaphore_enter (sem_t * sem)
{
	while (!sem->cur);
	if (sem->cur < 0)
		sem->cur = 0;
	while (!sem->cur);
	uint32 flags;
	save_flags (flags);
	cli ();
	sem->cur--;
	restore_flags (flags);
	return 0;
}

int semaphore_leave (sem_t * sem)
{
	sem->cur++;
	if (sem->cur > sem->max)
		sem->cur = sem->max;
	return 0;
}
